from django.db import models
from django.contrib.auth.models import AbstractUser


# Create your models here.
class UserInfo(AbstractUser):
    """用户表"""
    # # dynamic_num = models.BigIntegerField(verbose_name='动态数', default=0)
    # follow = models.
    # fans = CharField(verbose_name=' 粉丝数', max_length=11)
    openid = models.CharField(verbose_name='微信唯一标识', max_length=32, null=True)
    phone = models.CharField(verbose_name=' 手机号', max_length=11)
    avatar = models.CharField(verbose_name='头像', max_length=64, null=True, blank=True)
    token = models.CharField(verbose_name=' 用户Token', max_length=64, null=True, blank=True)
    # 创建时间
    creat_time = models.DateField(auto_now_add=True)

    class Meta:
        verbose_name_plural = '用户表'  # 修改admin后台管理默认的表名

    # verbose_name = '用户表'  # 默认是verbose_name加s

    def __str__(self):
        return self.username


class Category(models.Model):
    name = models.CharField(verbose_name='文章分类', max_length=32)
    hot_count = models.PositiveIntegerField(verbose_name='关注度', default=0)

    # def __str__(self):
    #     return self.name


class Tag(models.Model):
    name = models.CharField(verbose_name='文章标签', max_length=32)
    hot_count = models.PositiveIntegerField(verbose_name='使用次数', default=0)

    def __str__(self):
        return self.name


class Article(models.Model):
    title = models.CharField(verbose_name='文章标题', max_length=64)
    desc = models.CharField(verbose_name='文章简介', max_length=255)
    # 文章内容有很多-般情况下都是使用TextField
    content = models.TextField(verbose_name='文章内容')
    create_time = models.DateField(verbose_name='创建时间', auto_now_add=True)
    hot_count = models.PositiveIntegerField(verbose_name='关注度', default=0)

    # 数据库字段设计优化
    up_num = models.BigIntegerField(verbose_name='点赞数', default=0)
    down_num = models.BigIntegerField(verbose_name='点踩数', default=0)
    comment_num = models.BigIntegerField(verbose_name='评论数', default=0)

    # 外键字段(3个)
    user = models.ForeignKey(verbose_name='发布者', to='UserInfo', related_name='news', on_delete=models.CASCADE)
    category = models.ForeignKey(to='Category', null=True, on_delete=models.CASCADE)
    # 文章跟标签是多对多关系, 三种多对多创建方式, 我们选择自己创建第三张表
    tags = models.ManyToManyField(to='Tag',
                                  through='Article2Tag',
                                  through_fields=('article', 'tag')
                                  )

    def __str__(self):
        return self.title


class Article2Tag(models.Model):
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE)
    tag = models.ForeignKey(to='Tag', on_delete=models.CASCADE)


"""浏览记录表"""


class ViewerRecord(models.Model):
    news = models.ForeignKey(verbose_name='动态', to='Article', on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name='动态', to='UserInfo', on_delete=models.CASCADE)
    record_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)


"""动态点赞记录表"""


class NewsFavorRecord(models.Model):
    news = models.ForeignKey(verbose_name='动态', to='Article', on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name='动态', to='UserInfo', on_delete=models.CASCADE)
    favor_record_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)


class UpAndDown(models.Model):
    user = models.ForeignKey(to='UserInfo', on_delete=models.CASCADE)
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE)

    # 记录点赞还是点踩
    is_up = models.BooleanField()  # 传布尔值, 存0/1


class Comment(models.Model):
    user = models.ForeignKey(to='UserInfo', on_delete=models.CASCADE)
    article = models.ForeignKey(to='Article', on_delete=models.CASCADE)
    content = models.CharField(verbose_name='评论内容', max_length=255)
    comment_time = models.DateTimeField(verbose_name='评论时间', auto_now_add=True)
    # 自关联
    reply = models.ForeignKey(verbose_name='回复', to='self', null=True, blank=True, related_name='replys',
                              on_delete=models.CASCADE)
    depth = models.PositiveIntegerField(verbose_name="评论层级", default=1)
    # 自关联
    parent = models.ForeignKey(verbose_name='根评论', to='self', null=True, blank=True, related_name='parents',
                               on_delete=models.CASCADE)
    favor_count = models.PositiveIntegerField(verbose_name="赞数", default=0)
